package com.lagou.homework.game

class Game {
  //  游戏次数
  var totalNum: Int = _
  //  用户胜利次数
  var uWinNum: Int = _
  //  用户和局次数
  var uDrawNum: Int = _
  //  用户失败次数
  var uFailNum: Int = _

  //  电脑胜利次数
  var cWinNum: Int = _
  //  电脑失败次数
  var cFailNum: Int = _

  val user = new User
  val computer = new Computer

  def init(): Unit = {
    user.name = "游客"
    user.score = 0
    computer.name = "电脑"
    computer.score = 0
    totalNum = 0
    uWinNum = 0
    uDrawNum = 0
    uFailNum = 0
    cWinNum = 0
    cFailNum = 0
  }

  def play(index: Int): Unit = {
    if (index == 1 || index == 2 || index == 3) {
      totalNum += 1
      user.showFist(index)
      //随机生成1-3之间的数字
      val ranNum = scala.util.Random.nextInt(3) + 1
      computer.showFist(ranNum)
      //玩家赢
      if (index == 1 && ranNum == 3 || index == 2 && ranNum == 1 || index == 3 && ranNum == 2) {
        user.score += 2
        uWinNum += 1
        cFailNum += 1
        println("结果：你赢了！")
        //电脑赢
      } else if (index == 1 && ranNum == 2 || index == 2 && ranNum == 3 || index == 3 && ranNum == 1) {
        computer.score += 2
        cWinNum += 1
        uFailNum += 1
        println(s"结果： ${computer.name}赢！")
        //平局
      } else {
        uDrawNum += 1
        user.score +=1
        computer.score += 1
        println("结果：和局！")
      }
    }
  }

  def chooseComputer(index: Int): Unit = {
    index match {
      case 1 => computer.name = "刘备";
      case 2 => computer.name = "关羽";
      case 3 => computer.name = "张飞";
    }
  }

  def chooseUser(name: String): Unit = {
    user.name = name;
  }

  def result(): Unit = {
    println(s"${computer.name} VS ${user.name}")
    println(s"对战次数${totalNum}次")
    println("姓名    分数    胜局    和局    负局")
    println(s"${user.name}    ${user.score}      $uWinNum      $uDrawNum      $uFailNum")
    println(s"${computer.name}    ${computer.score}      $cWinNum      $uDrawNum      $cFailNum")
  }
}
